package com.lx.patterns.proxy.cglib_proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @ClassName ProxyFactory
 * @Description: //代理工厂
 * @Author: 落雪
 * @CreateDate: 2025/1/16 21:19
 * @UpdateUser: 落雪
 * @UpdateDate: 2025/1/16 21:19
 * @UpdateRemark:
 * @Version: 1.0
 */
public class ProxyFactory {


    public TrainStation getProxyObject(){

        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(TrainStation.class);

        enhancer.setCallback(new SellCallback());


        TrainStation proxy = (TrainStation)enhancer.create();

        return proxy;
    }
}

class SellCallback implements MethodInterceptor{

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("代理点收取一些服务费用(cglib动态代理)");
        return methodProxy.invokeSuper(o,objects);
    }
}
